package Initialize

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedis"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisStatus"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"go-fox-edge-kernel-gateway/controller"
	"go-fox-edge-kernel-gateway/entityManager"
	"go-fox-edge-kernel-gateway/routerServer"
)

func Initialize() {
	// 设置环境变量
	commEnv.Service.ServiceType = "kernel"
	commEnv.Service.ServiceName = "gateway-native"
	commEnv.Service.ModelType = "gateway"
	commEnv.Service.ModelName = "gateway"
	commEnv.Service.ApplicationName = "fox-edge-server-gateway-native"
	commEnv.SetAppPort(9101)
	commEnv.SetWorkPath(2)

	// 连接redis
	commRedis.ConnectRedis()

	// 检查连接状态：连接不上就通过panic退出吧
	ok := commRedis.TestConnect()
	if !ok {
		panic("连接redis失败:" + commRedis.Options.Addr)
	}

	commLogger.Info("------------- kernel-gateway-native 初始化开始！-------------")

	// 注册会用到的实体
	edgeEntity.Register()

	// 启动状态线程
	commRedisStatus.Instance()
	commRedisStatus.Scheduler()

	// 初始化数据管理
	entityManager.Instance()
	entityManager.InitLoadEntity()

	// 初始化配置参数
	initConfig()

	// 启动数据同步线程
	entityManager.Scheduler()

	// 启动路由
	routerServer.StartServer(controller.Topic)

	commLogger.Info("------------- kernel-gateway-native 初始化结束！-------------")

	commLogger.Info("------------- kernel-gateway-native 驻留在后台！-------------")
	select {}
}

func initConfig() {
}
